/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BeanView;

import Dao.DaoContract;
import Dao.DaoEmployee;
import HibernateUtil.HibernateUtil;
import Pojo.Contract;
import Pojo.Employees;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.context.RequestContext;

/**
 *
 * @author knyazev_o
 */
@ManagedBean
@ViewScoped
public class MbVContract implements java.io.Serializable{
    private Session session;
    private Transaction transaction;
    private Employees employees;
    private Contract contract;
    private List<Contract> listContract;
    private List<Contract> listContractFilter;
    
    
    @PostConstruct
    public void init() { 
        
    }
    
    public void registration()
    {
        this.session=null;
        this.transaction=null;
        
        try
        {            
            DaoContract daoContract=new DaoContract();           
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            daoContract.register(this.session, this.contract);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Договор зарегистрирован."));
            this.contract=new Contract();
            this.contract.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public List<Contract> getAll()
    {
        this.session=null;
        this.transaction=null;
        
        try
        {
            DaoContract daoContract=new DaoContract();            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();            
            this.listContract=daoContract.getAll(this.session);            
            this.transaction.commit();            
            return this.listContract;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));            
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public List<Contract> getAllMy()
    {
        this.session=null;
        this.transaction=null;
        
        try
        {
            DaoEmployee daoEmployee=new DaoEmployee();
            DaoContract daoContract=new DaoContract();            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();  
            HttpSession sessionEmployees=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            this.employees=daoEmployee.getByLogin(this.session, sessionEmployees.getAttribute("login").toString());
            this.listContract=daoContract.getAllMy(this.session, this.employees.getId());            
            this.transaction.commit();            
            return this.listContract;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));            
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }        
    }
    
    public void editorContract(Long id)
    {
        this.session=null;
        this.transaction=null;        
        try
        {
            DaoContract daoContract = new DaoContract();            
            this.session = HibernateUtil.getSessionFactory().openSession();
            this.transaction = session.beginTransaction();            
            this.contract = daoContract.getByIdContract(this.session, id);            
            RequestContext.getCurrentInstance().update("frmEditarUsuario:panelEditarUsuario");
            RequestContext.getCurrentInstance().execute("PF('dialogoEditarUsuario').show()");            
            this.transaction.commit();
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    } 
        
    public Contract getContract() {
        return contract;
    }

    public void setContract(Contract contract) {
        this.contract = contract;
    }

    public List<Contract> getListContract() {
        return listContract;
    }

    public void setListContract(List<Contract> listContract) {
        this.listContract = listContract;
    }

    public List<Contract> getListContractFilter() {
        return listContractFilter;
    }

    public void setListContractFilter(List<Contract> listContractFilter) {
        this.listContractFilter = listContractFilter;
    }



}
